package com.ztgf.sms.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.Md5Utils;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sms.models.dto.sms.SmsUserDTO;
import com.ztgf.sms.models.service.ISmsUserService;
import com.ztgf.sms.models.vo.SmsUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * 登陆
 *
 * @author YUE
 * @create 2020-05-27 17:32
 */
@RestController
@RequestMapping("/cms/login")
@Slf4j
@Api(value = "SmsLoginController",tags = "登陆")
public class SmsLoginController {

    @DubboReference
    private ISmsUserService SmsUserService;

    @ApiOperation(value = "退出登陆",notes = "退出登陆")
    @PostMapping(value = "/out")
    public Result quiteLogin(HttpServletRequest request){
        SmsUserService.delUserSession(CommonParameter.getHeadParamsBO().getSessionId());
        return new Result();
    }

    @ApiOperation(value = "登陆",notes = "登陆")
    @PostMapping(value = "/in")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号",required = true,paramType = "path", dataType = "String")
            ,@ApiImplicitParam(name = "password",value = "密码，md5加密",required = true,paramType = "query", dataType = "String")
    })
    public Result<SmsUserVO> login(String account, String password) {

        if(StrUtils.isEmpty(account) || StrUtils.isEmpty(password)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Wrapper wrapper = new EntityWrapper<SmsUserDTO>();
        wrapper.eq("account", account);
        SmsUserDTO dto = SmsUserService.selectOne(wrapper);

        String inputPassword = Md5Utils.MD5Encode(password.toUpperCase(), dto.getSalt());

        if(dto == null || !inputPassword.equalsIgnoreCase(dto.getPassword())){
            return new Result<>(CodeEnum.FAIL_USER_200000);
        }

        if(dto.getStatus() != 0){
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }

        SmsUserVO vo = new SmsUserVO();
        BeanUtils.copyProperties(dto,vo);

        String sessionId = Md5Utils.encryption(dto.getId() + "" + System.currentTimeMillis());

        vo.setSessionId(sessionId);
        dto.setPassword(null);
        dto.setSalt(null);
        //保存session进redis
        SmsUserService.setUserSession(sessionId, dto);
        return new Result<>(vo);
    }

}